package com.crazymaker.springcloud.demo.fallback;

import com.crazymaker.springcloud.demo.api.UAAUserApi;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.math.BigInteger;

/**
 * 用户降级工厂类
 */
@Slf4j
@Component
public class UAAUserFallbackFactory implements FallbackFactory<UAAUserApi> {
    @Override
    public UAAUserApi create(Throwable throwable) {
        log.info("服务报错：{}，进行降级", throwable.getMessage());
        return new UAAUserApi() {
            @Override
            public ResponseEntity<Object> getUser(BigInteger userId) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务出错");
            }
        };
    }
}
